<template>
  <!-- 首页 -->
  <div class="home">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav" fixed>
      <van-button
        slot="title"
        type="info"
        class="header-search"
        size="small"
        icon="search"
        to="/search"
        round
      >
        搜索
      </van-button>
    </van-nav-bar>

    <!-- /导航栏 -->

    <!-- tab标签 -->
    <van-tabs v-model="active" class="tab-nav" swipeable animated>
      <van-tab :title="item.name" v-for="item in pindaoList" :key="item.id">
        <Article :channles="item"></Article>
      </van-tab>
      <div slot="nav-right" class="zhanwei"></div>
      <div slot="nav-right" class="hanbao" @click="isShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /tab标签 -->

    <!-- 编辑汉堡弹出层 -->
    <van-popup
      v-model="isShow"
      position="bottom"
      closeable
      :style="{ height: '100%' }"
    >
      <ChanleEdit
        @updataActive="updataActive"
        @removeArticle="removeArticle"
        @addArticle="addArticle"
        :channleEdit="pindaoList"
        :active="active"
      ></ChanleEdit>
    </van-popup>
    <!-- /编辑汉堡弹出层 -->
  </div>
</template>

<script>
import { setItem, getItem } from "@/utils/storage.js";
import Article from "./components/article-list.vue";
import ChanleEdit from "./components/channle-edit.vue";
import { getPindao } from "@/api/user.js";
import { myArticle, myDelArticle } from "@/api/article.js";
import { mapState } from "vuex";
export default {
  created() {
    this.getList();
  },
  data() {
    return {
      active: 0,
      pindaoList: [],
      //编辑弹出层
      isShow: false,
    };
  },
  methods: {
    async getList() {
      if (this.user) {
        try {
          const { data: res } = await getPindao();
          this.pindaoList = res.data.channels;
          setItem("articleList", this.pindaoList);
        } catch (err) {
          console.log(err);
        }
      } else {
        this.pindaoList = getItem("articleList");
      }
    },
    // 子组件 发来 信号 修改active
    updataActive(index) {
      this.active = index;
    },
    //删除我的频道
    async removeArticle(id, index) {
      let i = this.pindaoList.findIndex((item) => item.id === id);
      i != -1 ? this.pindaoList.splice(i, 1) : "";
      if (index < this.active) {
        this.active--;
      }
      setItem("articleList", this.pindaoList);
      // 调用ajax 给数据更新一下
      if (this.user) {
        try {
          const { data: res } = await myDelArticle(id);
          console.log(res === "" ? "删除成功，但是接口不争气没用" : "");
        } catch (err) {
          console.log(err);
        }
      }
    },
    //添加我的频道
    async addArticle(val) {
      this.pindaoList.push(val);
      setItem("articleList", this.pindaoList);
      if (this.user) {
        try {
          const { data: res } = await myArticle({
            id: val.id,
            seq: this.pindaoList.length,
          });
          console.log(res ? "添加成功，但是接口不争气没用" : "");
        } catch (err) {
          console.log(err);
        }
      }
    },
  },
  computed: {
    ...mapState(["user"]),
  },
  components: {
    Article,
    ChanleEdit,
  },
};
</script>

<style lang='less' scoped>
/deep/ .van-nav-bar__title {
  max-width: 80%;
}

.header-search {
  width: 550px;
  font-size: 28px;
  background-color: #5babfb;
  border: none;
  i {
    font-size: 32px;
  }
}

/deep/ .tab-nav {
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    height: 82px;
    font-size: 30px;
    color: #777;
  }

  .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
  }
  .van-tab--active {
    color: #3296fa;
    font-weight: 700;
  }
  .van-tabs__line {
    background-color: #3296fa;
    padding-right: 0;
    position: fixed;
  }

  .hanbao {
    position: fixed;
    right: 0;
    width: 66px;
    height: 80px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    opacity: 0.9;
    i {
      font-size: 32px;
    }
    &::before {
      content: "";
      display: block;
      background-image: url("../../assets/gradient-gray-line.png");
      width: 1px;
      height: 100%;
      position: absolute;
      left: 0;
    }
  }
}
.zhanwei {
  width: 66px;
  height: 80px;
  flex-shrink: 0;
  background-size: contain;
}
</style>